import { getUserAnalysis } from '@/static/js/public.js'
import { baseUrl } from '@/utils/baseUrl.js'
import { parseTimeDot } from '@/utils/utils.js'
import { getList, getCate, getHotInformation } from '@/static/js/consult.js'
export default {
  data() {
    return {
      parseTimeDot,
      baseUrl,
      activeStyle: {
        color: '#4291FF',
        fontWeight: 'bold',
        transform: 'scale(1.05)'
      },
      inactiveStyle: {
        color: '#666666',
        transform: 'scale(1)'
      },
      itemStyle: {
        paddingLeft: '30rpx',
        paddingRight: '36rpx',
        height: '96rpx'
      },

      tabList: [], // tbs
      activeIndex: 0, // tabs下标
      scrollY: true,
      status: 'loadmore',
      keywords: ''
    };
  },
  onLoad(options) {
    this.getUserAnalysis()
    this.getCate() //获取分类
  },
  onShow() { },
  methods: {
    getUserAnalysis() {
      let params = {
        type: 2,
        m_id: 7
      }
      getUserAnalysis(params).then(res => {
        if (res.code == 1) {
          console.log('用户分析成功');
        }
      })
    },

    // 获取分类
    getCate() {
      getCate().then(({ data, msg, code }) => {
        if (code == 1) {
          data.splice(0, 0, {
            id: 0,
            name: "热门资讯",
            page: 1,
            pageSize: 10,
            dataList: [],
            nomore: false,
            triggered: false
          })
          data.map((item) => {
            item.triggered = false
            item.page = 1
            item.pageSize = 10
            item.dataList = []
            item.nomore = false
            return item
          })

          this.tabList = JSON.parse(JSON.stringify(data))
          this.getList()
        } else {
          this.$tools.msg(msg)
        }
      })
    },
    // 下拉刷新
    refresherrefresh() {
      this.tabList[this.activeIndex].triggered = true
      this.tabList[this.activeIndex].page = 1
      this.tabList[this.activeIndex].dataList = []
      this.getList()
    },
    // 上拉加载下一页
    scrolltolower() {
      if (this.tabList[this.activeIndex].nomore) return
      this.tabList[this.activeIndex].page++
      this.getList()
    },
    getList() {
      this.status = 'loading';
      if (this.tabList[this.activeIndex].id == 0) {
        this.getHotList();
        return;
      }
      let params = {
        tid: this.tabList[this.activeIndex].id,
        page: this.tabList[this.activeIndex].page,
        pageSize: this.tabList[this.activeIndex].pageSize
      };
      if (this.keyword) params.keyword = this.keyword;
      getList(params).then(({ data, msg, code }) => {
        // 加载更多提示处理
        console.log(data);
        if (data.length == 0 || data.length < 10) {
          this.status = 'nomore';
          this.tabList[this.activeIndex].nomore = true;
        } else {
          this.status = 'loadmore';
          this.tabList[this.activeIndex].nomore = false;
        }
        for (let item of this.tabList) {
          item.triggered = false;
        }
        if (code == 1) {
          if (data && data.length > 0) {
            let res = this.tabList[this.activeIndex];
            res.dataList.push(...data);
            this.tabList.splice(this.activeIndex, 1, res);
          }
        } else {
          this.$tools.msg(msg);
        }
      });
    },

    //热门资讯
    getHotList() {
      getHotInformation({
        page: this.tabList[this.activeIndex].page,
        pageSize: this.tabList[this.activeIndex].pageSize,
        keyword: this.keywords ? this.keywords : ''
      }).then(({
        data,
        msg,
        code
      }) => {
        // 加载更多提示处理
        if (data.length == 0 || data.length < 10) {
          this.status = 'nomore'
          this.tabList[this.activeIndex].nomore = true
        } else {
          this.status = 'loadmore'
          this.tabList[this.activeIndex].nomore = false
        }
        for (let item of this.tabList) {
          item.triggered = false
        }
        if (code == 1) {
          let res = this.tabList[this.activeIndex]
          res.dataList.push(...data)
          this.tabList.splice(this.activeIndex, 1, res)
        } else {
          this.$tools.msg(msg)
        }
      })
    },
    tabs(e) {
      console.log(e);
      this.activeIndex = e.index
      console.log(this.tabList);
    },
    //切换
    swiperChange(e) {
      let index = e.detail.current;
      this.activeIndex = index;
      if (this.tabList[this.activeIndex].dataList && this.tabList[this.activeIndex].dataList.length > 0) {
        return
      }
      this.getList()
    },
    backFn() {
      uni.navigateBack()
    },
    goDetails(e) {
      uni.navigateTo({
        url: './consultDetails?id=' + e.id
      });
    },
    transition(e) {
      this.scrollY = false
    },
    animationfinish(e) {
      this.scrollY = true
    }
  },
}